home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14401 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: solon.com!not-for-mail
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
  4. Subject: Re: C coding problem
  5. Date: 14 Apr 1996 10:53:37 -0500
  6. Organization: none
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4kr721$k7i@solutions.solon.com>
  10. References: <4ianbf$h86@solutions.solon.com> <4iemcl$a05@solutions.solon.com> <4io1io$no4@solutions.solon.com> <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com> <4kg8gl$iht@solutions.solon.com>
  11. Reply-To: fred@genesis.demon.co.uk
  12. NNTP-Posting-Host: solutions.solon.com
  13. X-NNTP-Posting-Host: genesis.demon.co.uk
  14. X-Newsreader: Demon Internet Simple News v1.27
  15. X-Mail2News-Path: genesis.demon.co.uk
  16.  
  17. In article <4kg8gl$iht@solutions.solon.com> mshan@ibm.net "Mike Shannon" writes:
  18.  
  19. >>Secondly, why not use arrays? In many places they are easier to
  20. >>understand - I for one am not going to compromise the readability
  21. >>of my programs because of some theoretical bull about them
  22. >>being second rate objects. (whatever that is intended to mean)
  23. >>a[i] is converted to *(a + i) by the compiler in any case so
  24. >>whats the advantage with your approach?
  25. >>
  26. >>Niall
  27. >
  28. >Sure, a[i] is the same as *(a+i). Both involve keeping up with
  29. >two variables, and, when i incremented, require a multiplication
  30. >of i times sizeof(*a) to add to a to find the address of the next
  31. >row.
  32.  
  33. Conceptually that's true. However it is rather unlikely that a current
  34. compiler would generate code that looks like that unless it is efficient.
  35. Strength reduction i.e. converting the loop to one based on pointers is
  36. a very standard optimisation these days. Where there is a difference I
  37. would expect the array based version to generate the more efficient code
  38. more often than not since you are giving the optimiser more information
  39. to work with. The key here is that a (the pointer) be invariant in the
  40. loop.
  41.  
  42. >But the example above is doing neither. p++ involves only one
  43. >variable, and only addition of sizeof(*p) to find the address of
  44. >the next row. Notice especially that there is no multiplication
  45. >required. Agreed the per event difference is small, but if a
  46. >program does a large number of array sweeps, the small
  47. >advantage adds up. Some optimizers may do this for you.
  48.  
  49. A compiler that doesn't is way behind the current state of play.
  50.  
  51. >I agree with simple syntax, though, and expect that something
  52. >like:
  53. >
  54. >    while (p < end_p)
  55. >    {    
  56. >         ++(*p);
  57.  
  58. What's wrong with ++*p?
  59.  
  60. >         p++;
  61. >    }
  62. >   
  63. >would produce the same assembly output as the above example,
  64. >and pose less interpretation risk in the future.
  65.  
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.